package edu.neu.chenhan.fragment.cooperation;

import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.EditText;
import android.widget.RadioGroup;
import android.widget.Toast;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;

import edu.neu.chenhan.R;
import edu.neu.chenhan.model.Meeting;

public class MeetingJoinFormFragment extends Fragment {
    private static final String ARG_MEETING = "meeting";
    private Meeting meeting;

    // 表单控件
    private EditText etCompany, etName, etPhone, etEmail, etArrivalMethod,
            etArrivalTrain, etArrivalTime;
    private RadioGroup rgGender;
    private Button btnSubmit;

    public static MeetingJoinFormFragment newInstance(Meeting meeting) {
        MeetingJoinFormFragment fragment = new MeetingJoinFormFragment();
        Bundle args = new Bundle();
        args.putSerializable(ARG_MEETING, meeting); // Meeting 需实现 Serializable
        fragment.setArguments(args);
        return fragment;
    }

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        if (getArguments() != null) {
            meeting = (Meeting) getArguments().getSerializable(ARG_MEETING);
        }
    }

    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.activity_attendance_form, container, false);

        // 初始化控件
        etCompany = view.findViewById(R.id.et_company);
        etName = view.findViewById(R.id.et_name);
        rgGender = view.findViewById(R.id.rg_gender);
        etPhone = view.findViewById(R.id.et_phone);
        etEmail = view.findViewById(R.id.et_email);
        etArrivalMethod = view.findViewById(R.id.et_arrival_method);
        etArrivalTrain = view.findViewById(R.id.et_arrival_train);
        etArrivalTime = view.findViewById(R.id.et_arrival_time);
        btnSubmit = view.findViewById(R.id.btn_submit);

        // 设置会议标题（可选）
        if (meeting != null) {
            // 假设在布局中有标题显示控件
            // tvMeetingTitle.setText(meeting.getTitle());
        }

        // 提交按钮点击事件
        btnSubmit.setOnClickListener(v -> submitForm());

        return view;
    }

    // 提交表单数据
    private void submitForm() {
        String company = etCompany.getText().toString().trim();
        String name = etName.getText().toString().trim();
        String phone = etPhone.getText().toString().trim();
        String email = etEmail.getText().toString().trim();
        String arrivalMethod = etArrivalMethod.getText().toString().trim();
        String arrivalTrain = etArrivalTrain.getText().toString().trim();
        String arrivalTime = etArrivalTime.getText().toString().trim();

        // 简单验证
        if (name.isEmpty()) {
            Toast.makeText(requireContext(), "请输入姓名", Toast.LENGTH_SHORT).show();
            return;
        }
        if (phone.isEmpty() || phone.length() != 11) {
            Toast.makeText(requireContext(), "请输入正确的手机号码", Toast.LENGTH_SHORT).show();
            return;
        }

        // TODO: 1. 收集表单数据
        // TODO: 2. 发送到服务器或本地存储
        // TODO: 3. 返回列表页或显示提交成功

        Toast.makeText(requireContext(), "参会信息提交成功", Toast.LENGTH_SHORT).show();
        getParentFragmentManager().popBackStack(); // 返回上一页
    }
}